package com.lifang.imgsoa.houseimagefile.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lifang.imgsoa.aop.BusinessException;
import com.lifang.imgsoa.base.controller.BaseController;
import com.lifang.imgsoa.houseimagefile.service.HouseImageFileService;
import com.lifang.imgsoa.houseimagefile.service.HuigouImageService;
import com.lifang.imgsoa.model.ImageKeyObject;
import com.lifang.imgsoa.util.ErrorEnum;
import com.lifang.imgsoa.util.SystemUtil;
import com.lifang.json.JsonTool;
import com.lifang.utils.statistics.StatisticsTimeMonitor;

@Controller
public class HouseImageFileController extends BaseController
{

    private Logger logger = LoggerFactory.getLogger(HouseImageFileController.class);

    private StatisticsTimeMonitor stm = new StatisticsTimeMonitor("HouseImageFileController.action");

    @Autowired
    private HuigouImageService huigouImageService;

    /**
     * @date 2014年5月20日 下午7:51:23
     * @author Tom
     * @return 返回key列表
     * @throws UnsupportedEncodingException
     * @description 单独上传图片接口，为其他应用提供
     * 
     * @param isMakeOriginal
     *            //是否对原图进行处理 fileBytes //文件的字节数组的字符串表示
     */
    @RequestMapping(value = "/uploadSingleFile.action", method = { RequestMethod.POST })
    @ResponseBody
    public void uploadSingleFile(HttpServletRequest req, HttpServletResponse resp, @RequestParam String fileBytes) throws UnsupportedEncodingException
    {
        if (fileBytes.length() == 0)
        {
            logger.info("fileBytes.length() == 0");
            throw new BusinessException(ErrorEnum.SOA_ERROR_100001);
        }
        Long timeBegin = System.currentTimeMillis();

        String key = huigouImageService.uploadSingleImage(fileBytes.getBytes("ISO8859-1"));
        ImageKeyObject imageKeyObject = huigouImageService.getImageKeyObject(key);

        stm.exeNormal(System.currentTimeMillis() - timeBegin);

        SystemUtil.writeResponse(req, resp, writeValueAsString(imageKeyObject));
    }

}
